﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MarginTrading.Entity;
using MarginTrading.Common;
using MarginTrading.Services;

namespace MarginTrading.Forms
{
    public partial class AllAdvisors : Form
    {
        public AllAdvisors()
        {
            InitializeComponent();
        }

        private void AllAdvisors_Load(object sender, EventArgs e)
        {
            try
            {
	            DataTable advisorTable = AdvisorServices.GetAdvisorCollectionDataTable();
	            dataGridViewUsers.DataSource = advisorTable;
	
	            dataGridViewUsers.Width = Size.Width - 40;
	            dataGridViewUsers.Height = Size.Height - 140;
            }
            catch (System.Exception ex)
            {
            	
            }

            //dataGridViewUsers.Columns[0].Width = (dataGridViewUsers.Width * 30) / 100;
            //dataGridViewUsers.Columns[1].Width = (dataGridViewUsers.Width * 30) / 100;
            //dataGridViewUsers.Columns[2].Width = (dataGridViewUsers.Width * 40) / 100;

            //System.Drawing.Size size = new System.Drawing.Size();
            //size.Height = dataGridViewUsers.Height + 100;
            //size.Width = 20;
            //this.MinimumSize = size;

            WindowState = FormWindowState.Maximized;
        }

        private void AllAdvisors_SizeChanged(object sender, EventArgs e)
        {
            dataGridViewUsers.Height = Size.Height - 140;
            dataGridViewUsers.Width = Size.Width - 40;
        }

        private void toolStripButtonAdd_Click(object sender, EventArgs e)
        {
            AddAdvisor oAddAdvisorDlg = new AddAdvisor();
            oAddAdvisorDlg.ShowDialog();

            try
            {
                DataTable advisorTable = AdvisorServices.GetAdvisorCollectionDataTable();
                dataGridViewUsers.DataSource = advisorTable;
                dataGridViewUsers.Refresh();
            }
            catch (System.Exception ex)
            {
            }
        }

        private void toolStripButtonDelete_Click(object sender, EventArgs e)
        {
            if (dataGridViewUsers.SelectedRows.Count == 0)
            {
                MessageBox.Show("No record selected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (dataGridViewUsers.SelectedRows.Count == 0)
            {
                MessageBox.Show("No record selected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            string advisorCode = dataGridViewUsers.SelectedRows[0].Cells[0].Value.ToString();
            DialogResult result = MessageBox.Show("Do you really want to delete advisor " + advisorCode + "?", "Confirmation", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

            if (result != DialogResult.Yes)
            {
                return;
            }

            try
            {
                AdvisorServices.Delete(advisorCode);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Failed to delete advisor\nError: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            dataGridViewUsers.Rows.RemoveAt(dataGridViewUsers.SelectedRows[0].Index);
            MessageBox.Show("Advisor deleted successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void toolStripButtonEdit_Click(object sender, EventArgs e)
        {
            if (dataGridViewUsers.SelectedRows.Count == 0)
            {
                MessageBox.Show("No record selected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (dataGridViewUsers.SelectedRows.Count == 0)
            {
                MessageBox.Show("No record selected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            EditAdvisor editAdvisorDlg = new EditAdvisor(dataGridViewUsers.SelectedRows[0].Cells[0].Value.ToString());
            editAdvisorDlg.ShowDialog();
            try
            {
                DataTable advisorTable = AdvisorServices.GetAdvisorCollectionDataTable();
                dataGridViewUsers.DataSource = advisorTable;
                dataGridViewUsers.Refresh();
            }
            catch (System.Exception ex)
            {	
            }
        }
    }
}
